#ifndef ASSERT_MACROS_H
#define ASSERT_MACROS_H

/*!
 * \file      assert_macros.h
 * \brief     Macros for debug.
 * \author    Duriez Franck
 * \author    Carton Paul-Henri
 * \version   0.1
 */

#include <iostream>
#include <sstream>
#include <QTextStream>
#include <QFile>

/*! \def ASSERT(condition, iftrue, iffalse)
 *  \param condition : The condition to verify
 *  \param iftrue    : The text to add in the log file if condition is true
 *  \param iffalse   : The text to add in the log file if condition is false
 */
/*{{{*/
#ifdef DEBUG
	#define ASSERT(condition,iftrue,iffalse) \
		do { \
			if (condition) { \
				if (iftrue != std::string("")) { \
					QFile debuglog("tetris.log"); \
					if (!debuglog.open(QIODevice::Append | QIODevice::Text)) \
						std::cout << "unable to open tetris.log" << std::endl; \
					else { \
						QTextStream flux(&debuglog); \
						flux << QString(iftrue) << QString("\n") ; \
						debuglog.close(); \
					} \
				} \
			} else { \
				QFile debuglog("tetris.log"); \
				if (!debuglog.open(QIODevice::Append | QIODevice::Text)) \
					std::cout << "unable to open tetris.log" << std::endl; \
				else { \
					QTextStream flux(&debuglog); \
					flux << QString("error at line ") << QString::number(__LINE__) \
					<< QString(" in file ") << QString(__FILE__) \
					<< QString(" : ") << QString(iffalse) << QString("\n"); \
					debuglog.close(); \
				} \
			} \
		} while(0);
#else
	#define ASSERT(condition,iftrue,iffalse)
#endif
/*}}}*/

/*! \def DEBUGGER
 *  \param string : The string to display in debug mode
 */
/*{{{*/
#ifdef DEBUG
	#define DEBUGGER(string) \
		do { \
			QFile debuglog("tetris.log"); \
			if (!debuglog.open(QIODevice::Append | QIODevice::Text)) \
				std::cout << "unable to open tetris.log" << std::endl; \
			else { \
				QTextStream flux(&debuglog); \
				flux << QString(string) << QString("\n"); \
				debuglog.close(); \
			} \
		} while(0);
#else
	#define DEBUGGER(string)
#endif
/*}}}*/

#endif // ASSERT_MACROS_H
